package com.wave.wavesystem.business.controller;

import com.mybatisflex.core.paginate.Page;
import com.wave.wavesystem.business.pojo.entity.DeptEntity;
import com.wave.wavesystem.business.pojo.vo.DeptVO;
import com.wave.wavesystem.business.service.DeptService;
import wave.common.pojo.entity.Query;
 import wave.common.pojo.entity.Result;
import wave.common.pojo.enums.RStatusEnum;
import com.wave.wavesystem.common.util.Condition;
import com.wave.wavesystem.common.util.Func;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 夕下风波
 * @date 2025-01-23
 * @description 部门表 访问层
 */
@RestController
@RequestMapping("/dept")
@Tag(name = "部门管理接口")
public class DeptController {

    @Resource
    private DeptService deptService;

    /**
     * 新增
     *
     * @param dept 新增实体
     */
    @Operation(summary = "新增")
    @PostMapping("/save")
    public Result<DeptEntity> save(@RequestBody DeptEntity dept) {
        deptService.save(dept);
        return Result.data(deptService.getById(dept.getId()), RStatusEnum.SUCCESS);
    }

    /**
     * 删除/批量删除
     *
     * @param ids id集合
     */
    @Operation(summary = "删除")
    @PostMapping("/remove")
    public Result<Boolean> remove(String ids) {
        List<String> idList = Func.strToStringList(ids);
        return Result.status(deptService.removeByIds(idList), RStatusEnum.SUCCESS);
    }

    /**
     * 更新
     *
     * @param dept 更新内容
     */
    @Operation(summary = "更新")
    @PostMapping("/update")
    public Result<DeptEntity> update(@RequestBody DeptEntity dept) {
        deptService.updateById(dept);
        return Result.data(deptService.getById(dept.getId()), RStatusEnum.SUCCESS);
    }

    /**
     * 详情查询
     *
     * @param dept 查询条件
     */
    @Operation(summary = "详情")
    @GetMapping("/detail")
    public Result<DeptEntity> detail(DeptEntity dept) {
        return Result.data(deptService.getById(dept.getId()));
    }

    /**
     * 列表查询
     *
     * @param dept 查询条件
     */
    @Operation(summary = "列表")
    @GetMapping("/list")
    public Result<List<DeptEntity>> list(DeptEntity dept) {
        return Result.data(deptService.list(Condition.getQueryWrapper(dept)));
    }

    /**
     * 条件分页查询
     *
     * @param dept  查询条件
     * @param query 分页条件
     */
    @Operation(summary = "分页")
    @GetMapping("/page")
    public Result<Page<DeptEntity>> page(DeptEntity dept, Query query) {
        return Result.data(deptService.page(new Page<>(query.getPageNum(), query.getPageSize()), Condition.getQueryWrapper(dept)));
    }

    /**
     * 获取所有部门
     */
    @Operation(summary = "获取所有部门")
    @GetMapping("/allDept")
    public Result<List<DeptVO>> allDept() {
        return Result.data(deptService.allDept());
    }

}